﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.IO.Ports;

namespace MultiRobot
{
    public partial class Form1 : Form
    {
        private ArrayList serials = new ArrayList();
        private delegate void print(string message);
        private delegate void refresh();
        private Thread robotthread;
        private Thread camthread;
        private RobotServer robotserver;
        private CamServer camserver;
       
        public Form1()
        {
            InitializeComponent();

            
            camserver = new CamServer();
            camthread = new Thread(camserver.DoWork);

            camthread.Start();
        }
        public void picturerefresh() {
            this.Invoke(new refresh(picrefresh), null);
            
        }
        public void picrefresh() {
            pictureBoxIpl1.Refresh();
            
        }
       
        public void printlog(string message) {
            this.Invoke(new print(additem), new object[] { message });
        }

        public void additem(string message){
            listBox1.Items.Add(message);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
            listBox1.Refresh();
        }

     

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
            if (camserver != null) {
                camthread.Abort();
                camserver.setDone();
            }
            
        }

        private void serialopen_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                string temp = serialPort1.ReadLine();
                Program.form1.printlog(temp);
            }
            else {
                serialPort1.Open();
                //robotserver = new RobotServer();
                //robotthread = new Thread(robotserver.DoWork);
                //robotthread.Start();
            }
            
        }

        private void serialclose_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                if (robotserver != null)
                {
                    robotserver.setDone();
                    robotthread.Abort();
                }
                serialPort1.Close();
            }
            else {
                Program.form1.printlog("COM3 already closed");
            }

        }



        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string temp = serialPort1.ReadLine();
            Program.form1.printlog(temp);
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Write("a");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.Write("b");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.Write("c");
        }

        private void pictureBoxIpl1_MouseUp(object sender, MouseEventArgs e)
        {
            
            
        }

        private void pictureBoxIpl1_MouseHover(object sender, EventArgs e)
        {


        }

        private void pictureBoxIpl1_MouseMove(object sender, MouseEventArgs e)
        {
            int x = (int)(e.X/10)+1;
            int y = (int)(e.Y/10)+1;
            
            toolTip1.SetToolTip(pictureBoxIpl1, x + ":" + y);
            
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
            
        }

        private void pictureBoxIpl1_Click(object sender, EventArgs e)
        {
            MouseEventArgs temp = (MouseEventArgs)e;
            int x = (int)(temp.X/10)+1;
            int y = (int)(temp.Y/10)+1;
            Program.form1.printlog(x + ":" + y);
        }

        

        


       
       
       

        
    }
}
